package com.bye.resolver;

import com.bye.dto.ManagerDto;
import com.bye.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;


//可以给方法中的形参赋值
@Component
public class AccountHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Autowired
    private ManagerService managerService;
    //支持给哪些类型的参数赋值, 或者这些参数是否要,满足某种条件
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.getParameterType().isAssignableFrom(ManagerDto.class);
    }

    //如何给参数赋值
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        String manager =(String) webRequest.getAttribute("manager", RequestAttributes.SCOPE_REQUEST);

        return managerService.getManagerDtoByAccountManager(manager) ;
    }
}
